package com.hctg.forced2sleep.dialog;

import android.content.Context;
import android.os.Bundle;

import com.actor.myandroidframework.dialog.ViewBindingDialog;
import com.actor.myandroidframework.utils.toaster.ToasterUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.hctg.forced2sleep.databinding.DialogIntEditBinding;

/**
 * description: int值修改
 * company    :
 *
 * @author : ldf
 * date       : 2024/12/1 on 15
 * @version 1.0
 */
public class IntEditDialog extends ViewBindingDialog<DialogIntEditBinding> {

    private final String title;
    private final int number;
    private final OnYesClickListener listener;

    /**
     * @param title 修改分钟数
     * @param number 1
     */
    public IntEditDialog(Context context, String title, int number, OnYesClickListener listener) {
        super(context);
        setWidthPercent(0.888888f, SizeUtils.dp2px(308f));
        this.title = title;
        this.number = number;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding.ivClose.setOnClickListener(v -> dismiss());
        viewBinding.tvCancel.setOnClickListener(v -> dismiss());
        viewBinding.tvYes.setOnClickListener(v -> {
            String trim = viewBinding.etContent.getText().toString().trim();
            try {
                int min = Integer.parseInt(trim);
                if (min > 0) {
                    dismiss();
                    if (listener != null) listener.onYesClick(min);
                } else {
                    ToasterUtils.error("请输入正确的数字");
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
                ToasterUtils.error("请输入正确的数字");
            }
        });

        viewBinding.tvTitle.setText(title);
        viewBinding.etContent.setText(String.valueOf(number));
    }

    public interface OnYesClickListener {
        void onYesClick(int number);
    }
}
